package com.ray.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class JsoupDownImg {

    public static void main(String[] args) throws IOException {
        String url = "http://www.ososxt.com/platform/xuan?tag=searchCourseList&type=75";
        Document document = Jsoup.parse(new URL(url), 10000);
        Element content = document.getElementById("crrimList");
        System.out.println(content);
        Elements imgs = content.getElementsByTag("img");
        System.out.println(imgs);
        int id = 0;
        for (Element img : imgs) {
            String src = img.attr("src");
            //获取输入流
            URL target = new URL(src);
            URLConnection urlConnection = target.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            id++;
            OutputStream outputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\常使用文件\\img\\jsoup\\"+id+".jpg");
            int temp = 0;
            while ((temp=inputStream.read())!=-1){
                outputStream.write(temp);
            }
            System.out.println(id+".jpg下载完毕");
            outputStream.close();
            inputStream.close();
        }

    }
}
